home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
drdobbs
/
c_spec
/
execute
/
cat.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-02-20
|
2KB
|
73 lines
#include <stdio.h>
#include <fcntl.h>
/* CAT.C Unlike the cat in K&R, this version grabs the
* biggest buffer it can from malloc, copies as
* much of the source file as it can with read().
* and writes to standard output with write().
* Files are opened in binary mode.
*
* Copyright (C) 1985, Allen I. Holub. All rights reserved.
*
* Usage: cat file...
*
* Exit status: The number of listed files that couldn't be opened
* successfully (0 if none).
*/
extern int open (char*,int);
extern int read (int, char*, unsigned);
extern int write (int, char*, int);
extern char *malloc (unsigned);
#define E(x) fprintf(stderr, "%s\n" ,x)
main(argc, argv)
char **argv;
{
char *buf;
int src_file ;
unsigned bsize = (1024 * 64) - 1 ;
register int rval = 0;
register int got;
ctlc();
reargv(&argc, &argv);
if( argc == 1 )
while( (got = getchar()) != EOF )
putchar( got );
else
{
if( argv[1][0] == '-' )
{
E("CAT: Copyright (c) 1986, Allen I. Holub\n");
E("Usage: cat <file list>\n");
E("Prints all files in list to standard output. Can be");
E("used to concatanate files by redirecting output ");
E("to another file, thus the name.");
exit( 0 );
}
while( !( buf = malloc(bsize)) && bsize >0 )
bsize -= 1024 ;
while( --argc > 0 )
{
if( (src_file = open(*++argv, O_RDONLY | O_TEXT)) == -1)
{
fprintf(stderr, "cat: can't open %s\n", *argv );
rval++;
continue;
}
while( (got = read(src_file, buf, bsize)) != 0 )
write( 1, buf, got );
}
}
putchar('\n');
exit(rval);
}